import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Messstation {
	
	//Repraesentiert quasi einen User, mit bezeichnung und pw//
	
	private String password = null;
	private int id = 0;
	private ArrayList<String> idlist = new ArrayList<String>();
	private ArrayList<String> passwordlist = new ArrayList<String>();
	private FileInputStream fis;
	private File f = new File("messstationen.txt");
	
	public Messstation()
	{
		//Standard thing with ID 0//
		this.id = 0;
	}
	
	public Messstation(String passwort, int bezeichnung)
	{
		//Good authenticated ID =)//
		this.password = passwort;
		this.id = bezeichnung;
		
		try 
		{
			//Datei existiert nicht -> neue erstellen//
			if(!f.exists())
			f.createNewFile();
			//Datei oeffnen//
			this.fis = new FileInputStream( "messstationen.txt" ); 
			DataInputStream in = new DataInputStream( fis );
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			  
			String strLine;
			while((strLine = br.readLine()) != null)
			{
				//Uname und pw delimited by " "//
				String line[] = strLine.split(" ");
									
				this.idlist.add(line[0]);
				this.passwordlist.add(line[1]);
			}
		}
		catch ( IOException e ) 
		{ }
		finally 
		{ 
			try 
			{ 
				this.fis.close(); 
			} 
			catch ( Exception e ) { }
		}
		
		if(!this.authenticate())
		{
			//Authentication failed, set everything back to 0//
			this.id = 0;
			this.password = null;
		}
	}
	
	public int getID()
	{
		return this.id;
	}
	
	public String getPassword()
	{
		return this.password;
	}
	
	private boolean authenticate()
	{
		for(int i = 0; i < this.idlist.size(); i++)
		{	
			//Go through all stored users and authenticate//
			if(this.idlist.get(i).equals(this.id) && this.passwordlist.get(i).equals(this.password))
				return true;
		}
		
		return false;
	}
}
